package com.lemon.smartsafetynew.net.services;

import android.content.Context;
import android.util.Log;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.BaseHttpServices;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew on 2017/7/18.
 */

public class AccountServices extends BaseHttpServices {

    public static final String TAG = "AccountServices";

    public AccountServices(Context context) {
        super(context);
    }

    /**
     * 初始化，等同使用构造函数
     *
     * @return
     */
    public static AccountServices init() {
        return new AccountServices(IApplication.context());
    }


    /**
     * 初始化，等同使用构造函数
     *
     * @param context
     * @return
     */
    public static AccountServices init(Context context) {
        return new AccountServices(context);
    }

    /**
     * 用户登录
     *
     * @param accountName
     * @param password
     */
    public void doLogin(String accountName, String password, String userId) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", accountName);
        phoneNumHashMap.put("passWord", password);
        phoneNumHashMap.put("userId", userId);
        HandlerCallback<List<User>> callback = new HandlerCallback<List<User>>(KeyConstants.URL_ACCOUNT_LOGIN) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ACCOUNT_LOGIN, Constants.GSAFE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);

    }

    /**
     * 用户注册-1
     *
     * 检查手机号的有效性
     *
     * @param telephoneNum
     */
    @Deprecated
    public void registerCheckTelephoneNum(String telephoneNum) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_REGISTER_TELEPHONE_NUM_CHECK) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.REGISTER_TELEPHONE_NUM_CHECK);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }

    /**
     * 用户注册-2
     *
     * 发送验证码
     *
     */
    @Deprecated
    public void registerAskAuthCode(String telephoneNum) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_REGISTER_ASK_AUTH_CODE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.REGISTER_ASK_AUTH_CODE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }

    /**
     * 用户注册-3
     *
     * 验证验证码
     *
     */
    @Deprecated
    public void registerCheckAuthCode(String telephoneNum, String authCode) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);
        phoneNumHashMap.put("code", authCode);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_REGISTER_CHECK_AUTH_CODE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.REGISTER_CHECK_AUTH_CODE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }

    /**
     * 用户注册-4
     *
     * 注册账号
     *
     */
    @Deprecated
    public void registerUser(HashMap<String, String> parameter) {

        HandlerCallback<User> callback = new HandlerCallback<User>(KeyConstants.URL_REGISTER_ACCOUNT) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.REGISTER_ACCOUNT);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(parameter)
                .build().execute(callback);
    }


    /**
     * 忘记密码-1
     *
     * 发送验证码
     *
     */
    @Deprecated
    public void forgetPasswordAskAuthCode(String telephoneNum) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_FORGET_PASSWORD_ASK_AUTH_CODE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.FORGET_PASSWORD_ASK_AUTH_CODE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }


    /**
     * 忘记密码-2
     *
     * 验证验证码
     *
     */
    @Deprecated
    public void forgetPasswordCheckAuthCode(String telephoneNum, String authCode) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);
        phoneNumHashMap.put("code", authCode);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_FORGET_PASSWORD_CHECK_AUTH_CODE) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.FORGET_PASSWORD_CHECK_AUTH_CODE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }

    /**
     * 忘记密码-3
     *
     * 重置密码
     *
     */
    @Deprecated
    public void forgetPasswordResetPassword(String telephoneNum, String newPassword) {

        HashMap<String,String> phoneNumHashMap = new HashMap<>();
        phoneNumHashMap.put("mobile", telephoneNum);
        phoneNumHashMap.put("newPwd", newPassword);

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_FORGET_PASSWORD_RESET_PASSWORD) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.FORGET_PASSWORD_RESET_PASSWORD);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(phoneNumHashMap)
                .build().execute(callback);
    }


    /**
     * 刷新用户数据
     */
    public void updateUserInfo(String token) {

        HandlerCallback<User> callback = new HandlerCallback<User>(KeyConstants.URL_UPDATE_USER_INFO) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.UPDATE_USER_INFO, Constants.GSAFE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .addParams("token", token)
                .build().execute(callback);
    }


    /**
     * 修改用户名
     */
    public void changeUserName(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_CHANGE_USER_NAME) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.CHANGE_USER_NAME, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }


    /**
     * 获取用户列表
     */
    public void getAccountList(String mobile, String password) {

        HandlerCallback<List<User>> callback = new HandlerCallback<List<User>>(KeyConstants.URL_GET_ACCOUNT_LIST) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_ACCOUNT_LIST);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .addParams("mobile", mobile)
                .addParams("passWord", password)
                .build().execute(callback);
    }

    /**
     * 修改用户密码
     */
    public void modifyAccountPassword(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_MODIFIED_PASSWORD) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.MODIFIED_PASSWORD, Constants.GSAFE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }

    /**
     * 获取账号用户组信息
     */
    public void getAccountUsers(LemonRequestParams params) {

        HandlerCallback<List<Map<String, String>>> callback = new HandlerCallback<List<Map<String, String>>>(KeyConstants.URL_GET_ACCOUNT_USERS) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.GET_ACCOUNT_USERS, Constants.GSAFE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }

    /**
     * 切换账号用户
     */
    public void toggleUser(LemonRequestParams params) {

        HandlerCallback<User> callback = new HandlerCallback<User>(KeyConstants.URL_TOGGLE_ACCOUNT_USER) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.TOGGLE_ACCOUNT_USER, Constants.GSAFE);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }


    /**
     * 绑定JPush服务
     */
    public void bindJPush(LemonRequestParams params) {

        HandlerCallback<String> callback = new HandlerCallback<String>(KeyConstants.URL_BIND_JPUSH) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.BIND_JPUSH, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }

    /**
     * 解绑JPush服务
     */
    public void unbindJPush(LemonRequestParams params, String key) {

        HandlerCallback<String> callback = new HandlerCallback<String>(key) {
        };
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.UNBIND_JPUSH, Constants.SERVER_V2);
        OkHttpUtils.post()
                .tag(callback.getTag())
                .url(serverUrl)
                .params(params)
                .build().execute(callback);
    }

}
